#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if (pid < 0)
    {
        std::cerr << "Fork failed" << std::endl;
        return 1;
    }
    else if (pid == 0)
    {
        // 子进程
        std::cout << "Child process with PID: " << getpid() << std::endl;
        sleep(5); // 子进程运行5秒
        exit(0);  // 子进程退出
    }
    else
    {
        // 父进程
        std::cout << "Parent process with PID: " << getpid() << std::endl;
        wait(NULL); // 父进程阻塞等待子进程退出
        std::cout << "Child process has exited" << std::endl;
    }
    return 0;
}